package com.canbot.u05.adapter.dance;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.dance.VoiceWord;

import java.util.List;

public class MicListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private static final String TAG = "MicListAdapter";

        private Context mContext;

        private List<VoiceWord> micList;

        private OnItemClickListener listener;

        private final int MIC_ADD_VIEW = 1;

        public MicListAdapter(Context context, List<VoiceWord> micList) {
                this.mContext = context;
                this.micList = micList;
        }

        public void addItemClickListener(OnItemClickListener listener) {
                this.listener = listener;
        }

        public void seMicList(List<VoiceWord> micList) {
                this.micList = micList;
                notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
                if ((position == micList.size()-1) && (micList.get(position).getName().equals("add"))){
                       return MIC_ADD_VIEW;
                }
                return super.getItemViewType(position);
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View view;
                if (i == MIC_ADD_VIEW){
                        view = LayoutInflater.from(mContext).inflate(R.layout.item_dance_detail_mic_add, viewGroup, false);
                        return new AddViewHolder(view);
                }else {
                        view = LayoutInflater.from(mContext).inflate(R.layout.item_dance_detail_mic_name, viewGroup, false);
                }

                return new MicViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                Log.d(TAG, "点击:"+position+" ," + micList.get(position));
                                listener.onItemClick(position);
                        }
                });
                if (viewHolder instanceof AddViewHolder){
                        AddViewHolder addViewHolder = (AddViewHolder)viewHolder;
                }else {
                        MicViewHolder micViewHolder = (MicViewHolder)viewHolder;
                        micViewHolder.micName.setText(micList.get(position).getName());
                        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                        listener.onItemLongClick(position);
                                        notifyDataSetChanged();
                                        return false;
                                }
                        });
                }
        }

        @Override
        public int getItemCount() {
                return micList == null ? 0 : micList.size();
        }

        public class MicViewHolder extends RecyclerView.ViewHolder {
                private TextView micName;

                public MicViewHolder(@NonNull View itemView) {
                        super(itemView);
                       micName = itemView.findViewById(R.id.dance_detail_mic_name);
                }
        }

        public class AddViewHolder extends RecyclerView.ViewHolder {
                private Button micAdd;

                public AddViewHolder(@NonNull View itemView) {
                        super(itemView);
                        micAdd= itemView.findViewById(R.id.dance_detail_mic_add);
                }
        }

        public interface OnItemClickListener {

                void onItemClick(int index);
                void onItemLongClick(int index);
        }
}
